#include"touchscr.h"

int iicfd;

void setupscr()
{//setupcode没有应答，返回-1
    int res;
    char reg;
    iicfd = open("/dev/i2c-0", O_RDWR); // I2C_DEV /dev/i2c-0
    if (iicfd < 0)
    {
        printf("####i2c test device open failed####/n");
        return (-1);
    }

    res = ioctl(iicfd, I2C_TENBIT, 0); //not 10bit
    printf("%d\n", res);
    res = ioctl(iicfd, I2C_TIMEOUT, 100000);
    printf("%d\n", res);
    struct i2c_rdwr_ioctl_data wsetup;
    struct i2c_msg setupreg;
    setupreg.addr = 0x4b;
    setupreg.len = 1;
    setupreg.flags = 0;
    setupreg.buf = &reg;
    wsetup.nmsgs = 1;
    wsetup.msgs = &setupreg;
    reg = 0xb0;
    res = ioctl(iicfd, I2C_RDWR, &wsetup);
    printf("%d\n", res);
}

short measurex()
{
    int res;
    char reg;
    struct i2c_rdwr_ioctl_data wsetup, readx;
    struct i2c_msg setupreg, measure;
    unsigned char volt[2];
    short voltx = 0;
    setupreg.addr = 0x4b;
    setupreg.len = 1;
    setupreg.flags = 0;
    setupreg.buf = &reg;
    wsetup.nmsgs = 1;
    wsetup.msgs = &setupreg;
    reg = 0xc8;
    res = ioctl(iicfd, I2C_RDWR, &wsetup);
    printf("%d\n", res);
    usleep(10);
    measure = setupreg;
    measure.len = 2;
    measure.flags = 1;
    measure.buf = volt;
    readx.nmsgs = 1;
    readx.msgs = &measure;
    res = ioctl(iicfd, I2C_RDWR, &readx);
    printf("%d\n", res);
    voltx = (volt[0] << 4) + (volt[1] >> 4);
    printf("%d\n", voltx);
    return voltx;
}

short measurey()
{
    int res;
    char reg;
    struct i2c_rdwr_ioctl_data wsetup, ready;
    struct i2c_msg setupreg, measure;
    unsigned char volt[2];
    short volty = 0;
    setupreg.addr = 0x4b;
    setupreg.len = 1;
    setupreg.flags = 0;
    setupreg.buf = &reg;
    wsetup.nmsgs = 1;
    wsetup.msgs = &setupreg;
    reg = 0xd8;
    res = ioctl(iicfd, I2C_RDWR, &wsetup);
    printf("%d\n", res);
    usleep(10);
    measure = setupreg;
    measure.len = 2;
    measure.flags = 1;
    measure.buf = volt;
    ready.nmsgs = 1;
    ready.msgs = &measure;
    res = ioctl(iicfd, I2C_RDWR, &ready);
    printf("%d\n", res);
    volty = (volt[0] << 4) + (volt[1] >> 4);
    printf("%d\n", volty);
    return volty;
}
unsigned char measurez1()
{
    int res;
    char reg;
    struct i2c_rdwr_ioctl_data wsetup, ready;
    struct i2c_msg setupreg, measure;
    unsigned char voltz1 = 0;
    setupreg.addr = 0x4b;
    setupreg.len = 1;
    setupreg.flags = 0;
    setupreg.buf = &reg;
    wsetup.nmsgs = 1;
    wsetup.msgs = &setupreg;
    reg = 0xea;
    res = ioctl(iicfd, I2C_RDWR, &wsetup);
    printf("%d\n", res);
    usleep(5);
    measure = setupreg;
    measure.len = 1;
    measure.flags = 1;
    measure.buf = &voltz1;
    ready.nmsgs = 1;
    ready.msgs = &measure;
    res = ioctl(iicfd, I2C_RDWR, &ready);
    printf("%d\n", res);
    printf("%d\n", voltz1);
    return voltz1;
}
unsigned char measurez2()
{
    int res;
    char reg;
    struct i2c_rdwr_ioctl_data wsetup, ready;
    struct i2c_msg setupreg, measure;
    unsigned char voltz2 = 0;
    setupreg.addr = 0x4b;
    setupreg.len = 1;
    setupreg.flags = 0;
    setupreg.buf = &reg;
    wsetup.nmsgs = 1;
    wsetup.msgs = &setupreg;
    reg = 0xfa;
    res = ioctl(iicfd, I2C_RDWR, &wsetup);
    printf("%d\n", res);
    usleep(5);
    measure = setupreg;
    measure.len = 1;
    measure.flags = 1;
    measure.buf = &voltz2;
    ready.nmsgs = 1;
    ready.msgs = &measure;
    res = ioctl(iicfd, I2C_RDWR, &ready);
    printf("%d\n", res);
    printf("%d\n", voltz2);
    return voltz2;
}
/*
int main()
{
    setupscr();
    measurex();
    measurey();
    measurez1();
    measurez2();
    return 0;
}*/
